package com.ruifu.doc.feign;


import com.fhzncloud.cloud.common.core.constant.SecurityConstants;
import com.fhzncloud.cloud.common.security.annotation.Inner;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.po.DmDocVer;
import com.ruifu.common.vo.worksheet.WsCommonSheet;
import com.ruifu.doc.mapper.DmDocVerMapper;
import com.ruifu.doc.service.PermissionsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文档类型控制层
 *
 * @author nyr
 * @create 2019/8/26
 */
@RestController
@CrossOrigin
@RequestMapping("/feignDoc")
public class FeignDmDoccController {
    @Autowired(required = false)
    DmDocVerMapper dmDocVerMapper;

    @Autowired(required = false)
    PermissionsService permissionsService;

    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    @GetMapping("/feignFindByIdDoc/{id}")
    @ApiOperation(value = "对外暴露文档详情", notes = "传入参数为文档主键id")
    public DmDocVer feignFindByIdDoc(@PathVariable("id") Long id){
        return dmDocVerMapper.findById(id);
    }

    @PostMapping("/feignUpdateDoc")
    @ApiOperation(value = "对外暴露文档修改", notes = "传入参数为文档对象实体")
    public void feignUpdateDoc(@RequestBody DmDocVer dmDocVer){
            dmDocVerMapper.update(dmDocVer,cloudUser);
    }

    @Inner
    @GetMapping("/feignCopyPermission/{sourceTenantId}/{newTenantId}")
    @ApiOperation(value = "对外暴露文档权限复制")
    public Boolean feignCopyPermission(@PathVariable("sourceTenantId") Integer sourceTenantId,
                                    @PathVariable("newTenantId") Integer newTenantId){
        return permissionsService.insertPermission(sourceTenantId,newTenantId);
    }

    @Inner
    @GetMapping("/feignCopyDocType/{sourceTenantId}/{newTenantId}")
    @ApiOperation(value = "对外暴露文档类型复制")
    public Boolean feignCopyDocType(@PathVariable("sourceTenantId") Integer sourceTenantId,
                                    @PathVariable("newTenantId") Integer newTenantId){
        return permissionsService.insertDocType(sourceTenantId,newTenantId);
    }

    @PostMapping("/feignWorkFormPermissions")
    @ApiOperation(value = "对外暴露工作单权限")
    public String feignWorkFormPermissions(@RequestBody WsCommonSheet wsCommonSheet){

        return  permissionsService.findPermissionByWorkFormId(wsCommonSheet,cloudUser);
    }
}
